package _1窗体;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class MyFrame2 extends JFrame {
	//
	public MyFrame2() {
		// 取消默认的布局方式
		this.setLayout(null);

		// 文本标签
		JLabel text1 = new JLabel("实现两个数的加减乘除");
		// 设定下在界面中的坐标
		text1.setBounds(180, 50, 150, 50);
		// 添加到窗体上
		this.add(text1);

		JLabel text2 = new JLabel("第一个数:");
		// 设定下在界面中的坐标
		text2.setBounds(100, 100, 100, 50);
		// 添加到窗体上
		this.add(text2);

		JLabel text3 = new JLabel("第二个数:");
		// 设定下在界面中的坐标
		text3.setBounds(100, 150, 100, 50);
		// 添加到窗体上
		this.add(text3);

		// 输入框
		JTextField input1 = new JTextField();
		input1.setBounds(200, 100, 100, 40);
		this.add(input1);

		JTextField input2 = new JTextField();
		input2.setBounds(200, 150, 100, 40);
		this.add(input2);

		// +按钮
		JButton btn1 = new JButton("+");
		btn1.setBounds(100, 200, 50, 40);
		this.add(btn1);
		// -
		JButton btn2 = new JButton("-");
		btn2.setBounds(160, 200, 50, 40);
		this.add(btn2);
		// *
		JButton btn3 = new JButton("*");
		btn3.setBounds(250, 200, 50, 40);
		this.add(btn3);
		// ÷
		JButton btn4 = new JButton("÷");
		btn4.setBounds(310, 200, 50, 40);
		this.add(btn4);
		
		JLabel resultText = new JLabel(" ");
		resultText.setBounds(200, 250, 80, 40);
		this.add(resultText);

		// 给按钮添加一个事件监听对象
		btn1.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub

				// 存放结果
				int result;

				// 输入框的值
				int a = Integer.parseInt(input1.getText());
				int b = Integer.parseInt(input2.getText());

				result = a + b;
				
				resultText.setText(" "+result);
				
				//JOptionPane.showMessageDialog(MyFrame2.this, "计算结果是" + result);

			}
		});
		btn2.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub

				// 存放结果
				int result;

				// 输入框的值
				int a = Integer.parseInt(input1.getText());
				int b = Integer.parseInt(input2.getText());

				result = a - b;

				JOptionPane.showMessageDialog(MyFrame2.this, "计算结果是" + result);

			}
		});
		btn3.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub

				// 存放结果
				double result;

				// 输入框的值
				double a = Integer.parseInt(input1.getText());
				double b = Integer.parseInt(input2.getText());

				result = a * b;

				JOptionPane.showMessageDialog(MyFrame2.this, "计算结果是" + result);

			}
		});
		btn4.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub

				// 存放结果
				double result;

				// 输入框的值
				double a = Integer.parseInt(input1.getText());
				double b = Integer.parseInt(input2.getText());

				result = a / b;

				JOptionPane.showMessageDialog(MyFrame2.this, "计算结果是" + result);

			}
		});
	}


}
